package cn.tedu.evaluation.management.controller;

import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.common.pojo.vo.PageData;
import cn.tedu.common.web.JsonResult;
import cn.tedu.evaluation.management.pojo.param.CommentAddNewParam;
import cn.tedu.evaluation.management.pojo.param.CommentUpdateParam;
import cn.tedu.evaluation.management.pojo.vo.CommentVO;
import cn.tedu.evaluation.management.service.ICommentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 处理评论相关请求的控制器类
 *
 * @author java@傲绝
 * @version 3.0
 */
@Slf4j
@RestController
@RequestMapping("/evaluations")
@Validated
@Api(tags = "1. 评论管理")
public class CommentController {
    @Autowired
    private ICommentService commentService;

//    @GetMapping("")


    @PostMapping("/add-new")
   // @PreAuthorize("hasAuthority('/content/article/simple')")
    @ApiOperation("发布评论")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal,
                             @ApiIgnore HttpServletRequest request,
                             @Valid CommentAddNewParam commentAddNewParam){
        log.debug("开始处理【发布评论】的请求，参数：{}", commentAddNewParam);

        commentService.addNew(currentPrincipal,commentAddNewParam);
        return JsonResult.ok();
    }


    @PostMapping("/{id}/userDelete")
    //@PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("根据ID删除评论")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的评论ID值！") Long id) {
        log.debug("开始处理【根据ID删除评论】的请求，参数：{}", id);
        commentService.delete(id);
        return JsonResult.ok();
    }



    @PostMapping("/{id}/update")
   // @PreAuthorize("hasAuthority('/content/category/simple')")
    @ApiOperation("修改评论")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的评论ID值！") Long id, CommentUpdateParam commentUpdateParam) {
        log.debug("开始处理【修改评论】的请求，ID：{}，新数据：{}", id, commentUpdateParam);
        commentService.updateInfoById(id, commentUpdateParam);
        return JsonResult.ok();
    }



    @PostMapping("/{id}/adminDelete")
    //@PreAuthorize("hasAuthority('/content/article/delete')")
    @ApiOperation("管理员根据ID删除评论")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "评论ID", required = true, dataType = "long")
    })
    public JsonResult adminDelete(@PathVariable @Range(min = 1, message = "请提交有效的评论ID值！") Long id) {
        log.debug("开始处理【根据ID删除评论】的请求，参数：{}", id);
        commentService.delete(id);
        return JsonResult.ok();
    }

//    @GetMapping("/")
//  //  @PreAuthorize("hasAuthority('/content/comment/simple')")
//    @ApiOperation("查询评论的评论列表")
//    @ApiOperationSupport(order = 421)
////   // @ApiImplicitParams({
////            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "int"),
////            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
////    })
//    public JsonResult listByComment() {
//        log.debug("开始处理【查询评论的评论列表】的请求");
//        List<CommentVO> commentVOS = commentService.listByComment();
//
//
//        return JsonResult.ok(commentVOS);
//    }




    @GetMapping("/")
    //  @PreAuthorize("hasAuthority('/content/comment/simple')")
    @ApiOperation("查询评论列表")
    @ApiOperationSupport(order = 421)
    public JsonResult listBySelect() {
        log.debug("开始处理【查询评论的评论列表】的请求");
        List<CommentVO> commentVOS = commentService.listBySelect();


        return JsonResult.ok(commentVOS);
    }




    @GetMapping("/anonymous")
    //  @PreAuthorize("hasAuthority('/content/comment/simple')")
    @ApiOperation("查询匿名的评论列表")
    @ApiOperationSupport(order = 521)
    public JsonResult anonymous() {
        log.debug("开始处理【查询匿名评论的评论列表】的请求");
        List<CommentVO> commentVOS = commentService.listAnonymousSelect();


        return JsonResult.ok(commentVOS);
    }


}
